import 'package:hive/hive.dart';
import 'package:uuid/uuid.dart';
import '../models/video_model.dart';

class VideoService {
  final Box<VideoModel> _videoBox = Hive.box<VideoModel>('videos');
  final Uuid _uuid = Uuid();

  // 创建视频
  Future<VideoModel> createVideo({
    required String title,
    required String description,
    required String videoUrl,
    required String thumbnailUrl,
  }) async {
    final video = VideoModel(
      id: _uuid.v4(),
      title: title,
      description: description,
      videoUrl: videoUrl,
      thumbnailUrl: thumbnailUrl,
    );

    await _videoBox.put(video.id, video);
    return video;
  }

  // 获取视频
  VideoModel? getVideoById(String id) {
    return _videoBox.get(id);
  }

  // 更新视频
  Future<void> updateVideo(VideoModel video) async {
    await _videoBox.put(video.id, video);
  }

  // 删除视频
  Future<void> deleteVideo(String id) async {
    await _videoBox.delete(id);
  }

  // 获取所有视频
  List<VideoModel> getAllVideos() {
    return _videoBox.values.toList();
  }

  // 切换收藏状态
  Future<void> toggleCollected(String id, bool isCollected) async {
    final video = getVideoById(id);
    if (video != null) {
      video.isCollected = isCollected;
      await updateVideo(video);
    }
  }

  // 搜索视频
  List<VideoModel> searchVideos(String query) {
    query = query.toLowerCase();
    return _videoBox.values.where((video) {
      return video.title.toLowerCase().contains(query) ||
          video.description.toLowerCase().contains(query);
    }).toList();
  }

  // 添加示例视频数据（开发测试用）
  Future<void> addSampleVideos() async {
    if (_videoBox.isEmpty) {
      await createVideo(
        title: 'Basic Diving Techniques',
        description:
            'Learn essential diving skills and safety knowledge for beginners',
        videoUrl: 'assets/videos/video1.mp4',
        thumbnailUrl: 'assets/images/knowledge2.png',
      );

      await createVideo(
        title: 'Coral Reef Diving Guide',
        description:
            'How to safely dive in coral reef areas while minimizing environmental impact',
        videoUrl: 'assets/videos/video2.mp4',
        thumbnailUrl: 'assets/images/knowledge1.png',
      );

      await createVideo(
        title: 'Deep Sea Diving Manual',
        description:
            'Professional deep diving techniques and equipment introduction',
        videoUrl: 'assets/videos/video3.mp4',
        thumbnailUrl: 'assets/images/knowledge3.png',
      );

      await createVideo(
        title: 'Underwater Photography Tips',
        description: 'How to capture stunning photos and videos underwater',
        videoUrl: 'assets/videos/video4.mp4',
        thumbnailUrl: 'assets/images/knowledge4.png',
      );

      await createVideo(
        title: 'Follow the turtles to swim',
        description:
            'Follow the turtles to swim among the corals and talk to the fish on the seabed. Every dive is an intimate contact with the ocean.',
        videoUrl: 'assets/videos/video5.mp4',
        thumbnailUrl: 'assets/images/share1.png',
      );
    }
  }
}
